<?PHP
 
class Admin_ku_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','库存管理');
		$this->assign('menuid1','3');
		$this->assign('menuid2','3');
		//权限判断
		factory::module('admin')->authSys('ku'); 
	}
	
 
	##################################
	/* 信息维护  */
	##################################
	function index__(){
	    
	 	$this->assign('nav','菜品库存'); 
		$db=factory::getDBO();
		$this->assign('rs',$db->loadassoclist('select * from `t_vegetype` order by show_index asc'));	
	}
	function index_list__(){ 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['type_id']))   $_POST['type_id']  = '';
			if(!isset($_POST['publishs']))    $_POST['publishs']  = '';  
  	  #查询条件
			$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name','b.type_name' ));
			if($_POST['type_id'] != ''){
				$sql_where .= ' and a.type_id="'.$_POST['type_id'].'" ';
			}
			if($_POST['sale']!=''){
				$sql_where .= ' and a.sale= "'.$_POST['sale'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_vege` a    left join t_vegetype b on a.type_id=b.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					a.yuns+a.nums as kutotal,
					b.type_name
				from `t_vege` a  left join t_vegetype b on a.type_id=b.id  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				
				
				foreach($rs as &$v){
					$v['sendtotal'] = $db->loadresult('select IFNULL(sum(vege_num),0) from t_send_vege a left join t_sendpi b on a.sendpi_id=b.id where a.vege_id="'.$v['id'].'" and  b.status in (0,1) ');
					
				}
				
				
				
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
	}
	
	public function  update__(){  
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			
			$db = factory::getDBO(); 
			$db->begin();
			$rs_update = array(
				'id'=>$form['id'],
				'nums'=>$form['nums'],
				'yuns'=>$form['yuns']
			);
			if(!$db->update('vege',$rs_update)){
				exit('操作失败！');
			}
			$db->commit();
			exit('{success:1}');
		}
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.*,b.type_name from `t_vege` a left join t_vegetype b on a.type_id=b.id  where a.deleted=0 and a.id="'.$id.'" ');
		if(!$rs){
			exit('没有查到任何记录！');
		}	
		$this->assign('rs',$rs);
	}
}
?>